#ifndef XGE_GRAPHICS_SPRITE_H_
#define XGE_GRAPHICS_SPRITE_H_
#include "xge_common_config.h"
#include "node_basenode.h"

#include "anim.h"
#include "anim_rec.h"

#include "g_common.h"
#include "g_colors.h"
#include "xtype.h"
#include "xallocator.h"

namespace xge{
namespace graphics{

  using xge::Point;
  using xge::Rect;

  // Here we will implement our graphics's 2D sprite
  // A sprite is a 2D texture on the screen ,so it
  // do not need the OnTick function callback
  class SpriteNode : public ListBaseNode , public AnimatorReceiver , public Allocator {
  public:
    // SpriteNode
    SpriteNode( const TexturePtr& texture , 
      const Rect & texture_rect ,
      const Point& anchor_point ,
      const Point& hv_scale ,
      const Point& position ,
      float  rotation , float zbuf , int blending = BLEND_DEFAULT ) ;
  public:
    void set_blending( int blending ) {
      quad_.blending_type = blending;
    }
    int blending () const {
      return quad_.blending_type;
    }
    virtual void set_z( float z ) {
      quad_.vertex[0].z 
        = quad_.vertex[1].z 
        = quad_.vertex[2].z
        = quad_.vertex[3].z
        =z;
    }
    virtual float z() const {
      return quad_.vertex[0].z;
    }

    virtual void set_color( const Color& color , int index = -1 ) {
      XASSERT( index == -1 || index >=0 && index <=3 );
      if( index == -1 ) {
        quad_.vertex[0].color 
          = quad_.vertex[1].color 
          = quad_.vertex[2].color
          = quad_.vertex[3].color
          =color.hardware_color();
      } else {
        quad_.vertex[index].color = color.hardware_color();
      }
    }

    virtual Color color( int index ) {
      return Color(quad_.vertex[0].color);
    }

    virtual void set_texture(  const TexturePtr& texture );
    virtual void set_texture_rect( const Rect& texture_rect );
    virtual void OnPaint( const Timer::tick_t& diff_time  );
  protected:
    // This function is used to paint the sprite only for RenderTargetNode
    // implementation 
    void PaintSpriteOnly( const Timer::tick_t& diff_time );
  protected:
    // The following function is used by the derived class which
    // can avoid the smart pointer using and it will be faster 
    SpriteNode(){}
  private:
    // update the quad status here
    void UpdateQuadStatus();
    // rendering interface 
    Quad quad_;
  };


}//namespace graphics
}//namespace xge

#endif// XGE_GRAPHICS_SPRITE_H_